/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.client.view;

import javax.swing.JOptionPane;
import com.agenda.client.ClienteServidor;
import com.agenda.client.requisicoes.ConfirmarRecusarCompromissoRequisicao;
import com.agenda.model.Compromisso;
import com.agenda.model.Convite;
import com.agenda.model.RespostaRequisicao;

/**
 *
 * @author Murilo
 */
public class ConfirmarRecusarDialogView extends javax.swing.JDialog {

	/**
	 * Creates new form ConfirmarRecusarDialogView
	 */
	public ConfirmarRecusarDialogView(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		opcaoCombo = new javax.swing.JComboBox();
		jLabel1 = new javax.swing.JLabel();
		salvarButton = new javax.swing.JButton();
		fecharButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Confirmar/Recusar Convite");
		setResizable(false);

		opcaoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ainda não sei.", "Eu vou aceitar.", "Eu vou recusar." }));

		jLabel1.setText("E sobre o convite");

		salvarButton.setText("Salvar");
		salvarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				salvarButtonActionPerformed(evt);
			}
		});

		fecharButton.setText("Fechar");
		fecharButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				fecharButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addGap(47, 47, 47).addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
					layout.createSequentialGroup().addGap(34, 34, 34).addComponent(salvarButton).addGap(18, 18, 18).addComponent(fecharButton))
					.addGroup(
						layout.createSequentialGroup().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
							.addComponent(opcaoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addGap(44, 44, 44).addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(opcaoCombo,
					javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(jLabel1)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE).addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(salvarButton).addComponent(fecharButton))
				.addContainerGap()));

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - 329) / 2, (screenSize.height - 182) / 2, 329, 182);
	}// </editor-fold>//GEN-END:initComponents

	private void salvarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarButtonActionPerformed
		salvar();
	}//GEN-LAST:event_salvarButtonActionPerformed

	private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
		fechar();
	}//GEN-LAST:event_fecharButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/*
		 * Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(ConfirmarRecusarDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(ConfirmarRecusarDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(ConfirmarRecusarDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(ConfirmarRecusarDialogView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/*
		 * Create and display the dialog
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				ConfirmarRecusarDialogView dialog = new ConfirmarRecusarDialogView(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {

					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton fecharButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JComboBox opcaoCombo;
	private javax.swing.JButton salvarButton;
	// End of variables declaration//GEN-END:variables

	private Compromisso compromisso;
	private Convite convite;

	private void salvar() {
		Convite novoConvite = new Convite();

		novoConvite.setResposta(opcaoCombo.getSelectedIndex());
		novoConvite.setUsuario(convite.getUsuario());

		try {
			RespostaRequisicao resposta = ClienteServidor.getINSTANCIA().enviarInformacao(
				new ConfirmarRecusarCompromissoRequisicao(compromisso, novoConvite));
			if (resposta.isErro()) {
				showMessage(resposta.getMsg());
				return;
			}
			showMessage("Convite atulizado com sucesso.");
			dispose();
		} catch (Exception e) {
			e.printStackTrace();
			showMessage(e.getMessage());
		} finally {
			ClienteServidor.getINSTANCIA().liberarComunicacao();
		}

	}

	private void showMessage(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}

	private void fechar() {
		dispose();
	}

	public void setCompromisso(Compromisso compromisso) {
		this.compromisso = compromisso;
	}

	public void setConvite(Convite convite) {
		this.convite = convite;
		opcaoCombo.setSelectedIndex(convite.getResposta());
	}
}
